<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>权限管理 - 管理员控制台</title>
    <link href="https://cdn.jsdelivr.net/npm/remixicon@3.5.0/fonts/remixicon.css" rel="stylesheet">
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
            background: #f5f7fa;
            min-height: 100vh;
        }
        .top-nav {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 0 30px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            z-index: 1000;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .logo {
            font-size: 24px;
            font-weight: bold;
            display: flex;
            align-items: center;
            gap: 10px;
            text-decoration: none;
            color: white;
        }
        .logout-btn {
            padding: 8px 20px;
            background: rgba(255,255,255,0.2);
            border: 1px solid rgba(255,255,255,0.3);
            color: white;
            border-radius: 20px;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 5px;
        }
        .container { margin-top: 80px; padding: 30px; }
        .page-header {
            margin-bottom: 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .page-title { font-size: 28px; color: #333; }
        .btn-primary {
            padding: 10px 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }
        .alert {
            padding: 12px 20px;
            border-radius: 8px;
            margin-bottom: 20px;
        }
        .alert-success { background: #d1fae5; border: 1px solid #10b981; color: #059669; }
        .alert-error { background: #fee2e2; border: 1px solid #ef4444; color: #dc2626; }
        .permissions-table-container {
            background: white;
            border-radius: 12px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
            overflow: hidden;
        }
        .table-header { padding: 20px; border-bottom: 1px solid #e0e0e0; }
        table { width: 100%; border-collapse: collapse; }
        thead { background: #f8f9fa; }
        th { padding: 15px; text-align: left; font-weight: 600; color: #666; font-size: 14px; }
        td { padding: 15px; border-top: 1px solid #f0f0f0; font-size: 14px; }
        tbody tr:hover { background: #f8f9fa; }
        .code-badge {
            padding: 4px 12px;
            border-radius: 12px;
            font-size: 12px;
            background: #e0e7ff;
            color: #4338ca;
            font-family: monospace;
        }
        .role-badge { 
            display: inline-block;
            padding: 3px 10px; 
            border-radius: 10px; 
            font-size: 11px; 
            margin: 2px;
            background: #fef3c7;
            color: #d97706;
        }
        .btn-small {
            padding: 6px 12px;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 12px;
            margin-right: 5px;
        }
        .btn-edit { background: #dbeafe; color: #2563eb; }
        .btn-delete { background: #fee2e2; color: #dc2626; }
        
        /* 模态框 */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0,0,0,0.5);
            z-index: 2000;
            align-items: center;
            justify-content: center;
        }
        .modal.show { display: flex; }
        .modal-content {
            background: white;
            border-radius: 12px;
            padding: 30px;
            max-width: 500px;
            width: 90%;
            max-height: 80vh;
            overflow-y: auto;
        }
        .modal-header { font-size: 20px; font-weight: 600; margin-bottom: 20px; color: #333; }
        .form-group { margin-bottom: 15px; }
        .form-group label { display: block; margin-bottom: 5px; font-size: 14px; color: #666; }
        .form-group input {
            width: 100%;
            padding: 10px;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            font-size: 14px;
        }
        .form-help {
            font-size: 12px;
            color: #999;
            margin-top: 5px;
        }
        .modal-actions {
            display: flex;
            gap: 10px;
            justify-content: flex-end;
            margin-top: 20px;
        }
        .btn-cancel { padding: 10px 20px; background: #f0f0f0; color: #666; border: none; border-radius: 8px; cursor: pointer; }
        .btn-confirm { padding: 10px 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border: none; border-radius: 8px; cursor: pointer; }
    </style>
</head>
<body>
    <!-- 顶部导航栏 -->
    <div class="top-nav">
        <a href="/admin/index" class="logo">
            <i class="ri-admin-line"></i>
            <span>管理员控制台</span>
        </a>
        <a href="/perform_logout" class="logout-btn">
            <i class="ri-logout-box-line"></i> 退出登录
        </a>
    </div>

    <!-- 主容器 -->
    <div class="container">
        <div class="page-header">
            <h1 class="page-title">权限管理</h1>
            <button class="btn-primary" onclick="showAddPermissionModal()">
                <i class="ri-key-line"></i> 添加权限
            </button>
        </div>

        <!-- 提示消息 -->
        <div class="alert alert-success" th:if="${success}" th:text="${success}"></div>
        <div class="alert alert-error" th:if="${error}" th:text="${error}"></div>

        <!-- 权限列表 -->
        <div class="permissions-table-container">
            <div class="table-header">
                <h3>权限列表（共 <span th:text="${permissions.size()}">0</span> 个）</h3>
            </div>

            <table>
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>权限代码</th>
                        <th>权限名称</th>
                        <th>关联角色</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr th:each="perm : ${permissions}">
                        <td th:text="${perm.id}">1</td>
                        <td>
                            <span class="code-badge" th:text="${perm.permCode}">perm:code</span>
                        </td>
                        <td th:text="${perm.permName}">权限名称</td>
                        <td>
                            <th:block th:each="role : ${perm.roles}">
                                <span class="role-badge" th:text="${role.roleName}">角色</span>
                            </th:block>
                            <span th:if="${#lists.isEmpty(perm.roles)}" style="color: #999;">未分配</span>
                        </td>
                        <td>
                            <button class="btn-small btn-edit" 
                                    th:attr="data-id=${perm.id}, data-permcode=${perm.permCode}, data-permname=${perm.permName}"
                                    onclick="editPermission(this)">
                                <i class="ri-edit-line"></i> 编辑
                            </button>
                            <button class="btn-small btn-delete" 
                                    th:attr="data-id=${perm.id}, data-permcode=${perm.permCode}, data-permname=${perm.permName}, data-hasroles=${!#lists.isEmpty(perm.roles)}"
                                    onclick="deletePermission(this)">
                                <i class="ri-delete-bin-line"></i> 删除
                            </button>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

    <!-- 添加权限模态框 -->
    <div id="addPermissionModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">添加新权限</div>
            <form action="/admin/permissions/add" method="post">
                <div class="form-group">
                    <label>权限代码 *</label>
                    <input type="text" name="permCode" required placeholder="例如: problem:add, contest:manage">
                    <div class="form-help">格式：模块:操作，使用小写字母和冒号</div>
                </div>
                <div class="form-group">
                    <label>权限名称 *</label>
                    <input type="text" name="permName" required placeholder="例如: 添加题目, 管理竞赛">
                    <div class="form-help">简短描述该权限的功能</div>
                </div>
                <div class="modal-actions">
                    <button type="button" class="btn-cancel" onclick="closeAddPermissionModal()">取消</button>
                    <button type="submit" class="btn-confirm">添加</button>
                </div>
            </form>
        </div>
    </div>

    <!-- 编辑权限模态框 -->
    <div id="editPermissionModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">编辑权限</div>
            <form id="editPermissionForm" method="post">
                <input type="hidden" id="editPermissionId">
                <div class="form-group">
                    <label>权限代码 *</label>
                    <input type="text" id="editPermCode" name="permCode" required>
                    <div class="form-help">格式：模块:操作，使用小写字母和冒号</div>
                </div>
                <div class="form-group">
                    <label>权限名称 *</label>
                    <input type="text" id="editPermName" name="permName" required>
                    <div class="form-help">简短描述该权限的功能</div>
                </div>
                <div class="modal-actions">
                    <button type="button" class="btn-cancel" onclick="closeEditPermissionModal()">取消</button>
                    <button type="submit" class="btn-confirm">保存</button>
                </div>
            </form>
        </div>
    </div>

    <script>
        function showAddPermissionModal() {
            document.getElementById('addPermissionModal').classList.add('show');
        }
        
        function closeAddPermissionModal() {
            document.getElementById('addPermissionModal').classList.remove('show');
        }
        
        function editPermission(btn) {
            const id = btn.getAttribute('data-id');
            const permCode = btn.getAttribute('data-permcode');
            const permName = btn.getAttribute('data-permname');
            
            document.getElementById('editPermissionId').value = id;
            document.getElementById('editPermCode').value = permCode;
            document.getElementById('editPermName').value = permName;
            document.getElementById('editPermissionForm').action = '/admin/permissions/edit/' + id;
            document.getElementById('editPermissionModal').classList.add('show');
        }
        
        function closeEditPermissionModal() {
            document.getElementById('editPermissionModal').classList.remove('show');
        }
        
        function deletePermission(btn) {
            const id = btn.getAttribute('data-id');
            const permName = btn.getAttribute('data-permname');
            const hasRoles = btn.getAttribute('data-hasroles') === 'true';
            
            if (hasRoles) {
                alert('该权限正在被某些角色使用，无法删除！\n\n请先从相关角色中移除此权限。');
                return;
            }
            
            if (confirm('确定要删除权限 "' + permName + '" 吗？\n\n此操作不可撤销！')) {
                const form = document.createElement('form');
                form.method = 'POST';
                form.action = '/admin/permissions/delete/' + id;
                document.body.appendChild(form);
                form.submit();
            }
        }
        
        window.onclick = function(event) {
            if (event.target.classList.contains('modal')) {
                closeAddPermissionModal();
                closeEditPermissionModal();
            }
        }
        
        // 自动隐藏提示
        setTimeout(() => {
            document.querySelectorAll('.alert').forEach(alert => {
                alert.style.display = 'none';
            });
        }, 5000);
    </script>
</body>
</html>
